<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    lineChartData: {
      type: Array,
      required: true
    }
  },
  watch:{
    lineChartData: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart()
    // })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(arr) {
      let dateArr = [];
      let dataArr = [];

      if(arr.length > 0){
        arr.forEach(item=>{
          dateArr.push(item['date']);
          dataArr.push(item['count'])
        })
      }

      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title:{
          text:'近7日挂号预约情况'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
           name:'日期',
          type: 'category',
          data: dateArr,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
           name:'预约人次',
          type: 'value',
          axisTick: {
            show: false
          },
           axisLine: {
                      show: false // 隐藏Y轴的竖线
                  }
        }],
        series: [{
          name: 'pageA',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: dataArr,
          animationDuration,
          itemStyle:{
              color: new echarts.graphic.LinearGradient(
                            0, 0, 0, 1, // 渐变方向：从上到下
                            [
                                {offset: 0, color: '#00ffff'},   // 柱图顶部颜色
                                {offset: 1, color: '#0000ff'}    // 柱图底部颜色
                            ]
                        )
          }
        }]
      })
    }
  }
}
</script>
